﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarvellousWorks.PracticalPattern.FactoryMethod.GenericFactory
{
    public class ProductCollection
    {
        private IList<IProduct> data = new List<IProduct>();
        public void Insert(IProduct product)
        {
            data.Add(product);
        }
        public void Insert(IProduct[] products)
        {
            foreach (var item in products)
            {
                data.Add(item);
            }
        }

        public void Remove(IProduct product)
        {
            data.Remove(product);
        }

        public void Clear() { data.Clear(); }

        public IProduct[] Data
        {
            get
            {
                if (data == null || data.Count == 0) return null;
                IProduct[] result = new IProduct[data.Count];
                data.CopyTo(result, 0);
                return result;
            }
        }

        public int Count { get { return data.Count; } }

        // 为了便于操作重载运算符
        public static ProductCollection operator +(ProductCollection collection, IProduct[] products)
        {
            ProductCollection result = new ProductCollection();
            if (collection != null && collection.Count > 0)
            {
                result.Insert(collection.Data);
            }
            if (products != null && products.Length > 0)
            {
                result.Insert(products);
            }
            return result;
        }

        public static ProductCollection operator +(ProductCollection source, ProductCollection target)
        {
            ProductCollection result = new ProductCollection();
            if (source != null && source.Count > 0)
            {
                result.Insert(source.Data);
            }
            if (target != null && target.Count > 0)
            {
                result.Insert(target.Data);
            }
            return result;
        }
    }
}
